package io.finer.erp.jeecg.bas.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;

public class ProductPriceGroupUtil {
	public static List<Map<String,Object>> getProductPriceGroup(List<Double> str1, Double min, Double max) {
		List<Map<String,Object>> pds = new ArrayList<>();
		diguiSum(arrayTransform(str1,max),min,max,pds);
		return pds;
	}

	public static void diguiSum(List<Double> str, Double min, Double max,List<Map<String,Object>> pds) {
		for (int i = 0; i < 10; i++) {
			Double[] cache = new Double[i + 1];
			int ceng = -1;
			int cengQuit = i;
			int startPiont = 0;
			cir(ceng, cengQuit, startPiont, str, cache, min,max,pds);
		}
	}

	// 递归求结果
	public static void cir(int ceng, int cengQuit, int startPiont, List<Double> array, Double[] cache, Double min,Double max,List<Map<String,Object>> pds) {
		ceng++;
		for (int i = startPiont; i < array.size(); i++) {
			cache[ceng] = array.get(i);
			if (ceng == cengQuit) {
				if ((getSum(cache) >= min) && (getSum(cache) <= max)) {
					pds.add(printcache(cache));
				}
				if ((getSum(cache) < min) || (getSum(cache) > max)) {
					continue;
				}
			}
			if (ceng < cengQuit) {
				startPiont = i;
				cir(ceng, cengQuit, startPiont, array, cache,min,max,pds);
			}
		}
	}

	// 获取组合数字之和
	public static Double getSum(Double[] cache) {
		Double sum = 0.00;
		for (int i = 0; i < cache.length; i++) {
			sum = sum + cache[i];
		}
		return sum;
	}

	// 打印组合的可能
	public static Map<String,Object> printcache(Double[] cache) {
		Map<String,Object> pd = new HashedMap();
		Double sum = 0.00;
		String s = "";
		for (int i = 0; i < cache.length; i++) {
			if(i != cache.length - 1){
				s += (cache[i] + ",");
				sum += cache[i];
			}else{
				s += cache[i];
				sum += cache[i];
			}
		}
		pd.put("price_sum",sum);
		pd.put("price_group",s);
		pd.put("group_size",cache.length);
		return pd;
	}
	public static List<Double> arrayTransform(List<Double> strArray,Double max) {
		for (int i = 0; i < strArray.size() - 1; i++) {
			for (int j = 1; j < strArray.size() - i; j++) {
				Double a;
				if ((strArray.get(j - 1)).compareTo(strArray.get(j)) > 0) { // 比较两个整数的大小
					a = strArray.get(j - 1);
					strArray.set((j - 1), strArray.get(j));
					strArray.set(j, a);
				}
			}
		}
		for (int i = 0; i < strArray.size(); i++) {
			if (strArray.get(i) > max) {
				strArray.remove(i);
			}
		}
		return strArray;
	}
	
	public static void main(String[] args) {
        List<Double> str1 = new ArrayList<Double>();
        str1.add(27.0);
        str1.add(26.0);
        str1.add(28.0);
        str1.add(29.0);
        str1.add(30.0);
        
        List<Integer> str2=new ArrayList<Integer>();
        str2.add(4);
        str2.add(10);
        str2.add(20);
        str2.add(40);
        str2.add(30);
        
        Map<Double,Integer> mmm=new HashMap<Double,Integer>();
        mmm.put(27.0, 4);
        mmm.put(26.0, 10);
        mmm.put(27.0, 20);
        mmm.put(28.0, 40);
        mmm.put(29.0, 30);
        
        Map<Integer,String> tarMap=new HashMap<Integer,String>();
 
        Double min = 107.00;
        Double max = 108.00;
        List<Map<String,Object>> result = ProductPriceGroupUtil.getProductPriceGroup(str1,min,max);
        for(int k=0;k<result.size();k++){
        	Map<String,Object> map=result.get(k);
        	Object ss=map.get("price_group");
        	String [] s=ss.toString().split(",");
        	Map<Double,Integer> m = new HashMap<Double,Integer>();
        	for(String t:s) {
        		try {
        			Integer tt=m.get(Double.parseDouble(t));
        			m.put(Double.parseDouble(t), tt+1);
        		}catch(Exception e) {
        			m.put(Double.parseDouble(t),1);
        		}
        	}
        	String st="";
        	for(Double key : m.keySet()){
        	    Integer value = m.get(key);
        	    if(st.equals("")) {
        	    	st=key+"*"+value;
        	    }else {
        	    	st+="+"+key+"*"+value;
        	    }
        	}
        	tarMap.put(k, st);
            System.out.println(st+"\r\n");
        }
        String fa1="";
        String fa2="";
        String fa3="";
        String fa4="";
        //判断最多方案
        if(null!=tarMap&&tarMap.size()>0) {
        	for(Integer key : tarMap.keySet()){
        	    String value = tarMap.get(key);
        	    String [] tmp=value.split("\\+");
        	    if(tmp.length==4) {
        	    	fa4=value;
        	    	break;
        	    }
        	}
        	for(Integer key : tarMap.keySet()){
        	    String value = tarMap.get(key);
        	    String [] tmp=value.split("\\+");
        	    if(tmp.length==3) {
        	    	fa3=value;
        	    	break;
        	    }
        	}
        	for(Integer key : tarMap.keySet()){
        	    String value = tarMap.get(key);
        	    String [] tmp=value.split("\\+");
        	    if(tmp.length==2) {
        	    	fa2=value;
        	    	break;
        	    }
        	}
        	for(Integer key : tarMap.keySet()){
        	    String value = tarMap.get(key);
        	    String [] tmp=value.split("\\+");
        	    if(tmp.length==1) {
        	    	fa1=value;
        	    	break;
        	    }
        	}
        	System.out.println("获取的最多方案==="+fa3);
        	if(!fa4.equals("")) {
        		
        	}else if(!fa3.equals("")) {
        		String [] tt=fa3.split("\\+");
        		for(String t:tt) {
        			Integer num=mmm.get(Double.parseDouble(t));
        			String [] ttt=t.split("*");
        			//给定方案与目标数量不一致，修改为目标数量
        			if(Integer.parseInt(ttt[1])<num) {
        				mmm.put(Double.parseDouble(t), Integer.parseInt(ttt[1]));
        			}
        		}
        		//计算最大倍数
        		
        	}
        }
    }

}